﻿/**********************************************
 * 功能名称：资源动因录入页面
编写人：  杨莹
建立时间： 杨 2008年11月26日
说明：
加载页面时，列出当前操作员的责任范围的作业中心列表。
查询出选择的作业中心对应的目标产品的动因量。
 * 录入后，提交保存。

 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;

namespace Hisun.View.CostDriver
{
    public partial class CostDriverForProductInput : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

        
                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                DropDownList_CostCenter.Items.Clear();

                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                HiddenField_CurrentCenterId.Value = "";

            }
        }


        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => p.类型=="A" && p.停用 == false && p.是否直接成本 == true);


            foreach (var MylistItem in myCenterFilters)
            {
                if (MylistItem.作业中心名称.Contains(TextBox_FilterText.Text))
                {
                    ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                    DropDownList_CostCenter.Items.Add(InsertItem);
                }
            }


        }

        protected void Button_fullCenterList_Click(object sender, EventArgs e)
        {
            var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
            DropDownList_CostCenter.Items.Clear();
            ListItem InsertItem = new ListItem("请选择", "");
            DropDownList_CostCenter.Items.Add(InsertItem);
            FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

            HiddenField_CurrentCenterId.Value = "";
        }

        protected void Button_Query_Command(object sender, CommandEventArgs e)
        {
              //绑定数据
            if (DropDownList_CostCenter.SelectedValue == "")
            {
                Response.Write("<script>alert('无作业中心');</script>");
                HiddenField_CurrentCenterId.Value = "";
                return;
            }
            HiddenField_CurrentCenterId.Value = DropDownList_CostCenter.SelectedValue;
            DataBind(HiddenField_CurrentCenterId.Value);
            if (GridView_DriverList.Rows.Count == 0)
            {
                Panel_GridList.Visible = false;

            }
            else
            {
                Panel_GridList.Visible = true;
            }
        }


        private void DataBind(string SelectCenterId)
        {
            int MyCenterId = Convert.ToInt32(SelectCenterId);
            var MyDataQuery = from a in db.TABC_JS_ProductInProcess
                              join b in ((from e in db.TABC_JS_ProductCostDriver where e.会计期间ID==Application["accountPeriod"].ToString() select e))
                                    on a.中间体ID equals b.中间体ID    into mytemp
                              where  a.作业中心ID == MyCenterId 
                              &&  a.可用 == true 
                              from temp in mytemp.DefaultIfEmpty()
                              select new
                              {
                                  a.中间体ID,
                                  a.中间体名称,
                                  动因量 = (temp == null ? 0 : temp.动因量)

                              };
            GridView_DriverList.DataSource = MyDataQuery;
            GridView_DriverList.DataBind();


        }



        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            int MyCenterId = Convert.ToInt32(HiddenField_CurrentCenterId.Value);

            var IsLock = (from a in db.TABC_JS_ProductCostDriver
                          where a.作业中心ID == MyCenterId
                          && a.会计期间ID == Application["accountPeriod"].ToString()
                          && a.锁定 == true
                          select a).Count();
            if (IsLock > 0)
            {

                alert = "<script>alert('当前作业中心被锁定');</script>";
                return;
            }

            if (!Page.IsValid)
            {
               alert = "<script>alert('非法数据输入,请修改');</script>";
               return;
            }


            try
            {

             
       
                 //对当前列表的中间体动因记录进行插入
                for (int i = 0; i < GridView_DriverList.Rows.Count; i++)
                {
                    GridViewRow myrows = GridView_DriverList.Rows[i];
                    var IsExistRow = from a in db.TABC_JS_ProductCostDriver
                                     where a.会计期间ID == Application["accountPeriod"].ToString()
                                         && a.作业中心ID == MyCenterId
                                         && a.中间体ID == myrows.Cells[0].Text.Trim()
                                     select a;
                    if (IsExistRow.Count() == 0)
                    {
                        TABC_JS_ProductCostDriver myNewRow = new TABC_JS_ProductCostDriver();
                        myNewRow.公司ID = "hisun";
                        myNewRow.账套ID = "001";
                        myNewRow.作业中心ID = MyCenterId;
                        myNewRow.中间体ID = myrows.Cells[0].Text.Trim();
                        myNewRow.会计期间ID = Application["accountPeriod"].ToString();
                        myNewRow.录入人 = Session[ServerVars.sysSessionUserName].ToString();

                        string mynum = ((TextBox)myrows.Cells[2].FindControl("TextBox_DriverAmount")).Text;
                        myNewRow.动因量 = Convert.ToDecimal(mynum);

                        db.TABC_JS_ProductCostDriver.InsertOnSubmit(myNewRow);
                    
                    }
                    else if (IsExistRow.Count() == 1)
                    {
                        var myEditRow = IsExistRow.Single();
                        string mynum = ((TextBox)myrows.Cells[2].FindControl("TextBox_DriverAmount")).Text;
                        myEditRow.动因量 = Convert.ToDecimal(mynum);
                        myEditRow.录入人 = Session[ServerVars.sysSessionUserName].ToString();
                    
                    }
       
                }
                 
                var  DelUnusedPIPDriverAccount = from m in db.TABC_JS_ProductCostDriver
                                                 join n in db.TABC_JS_ProductInProcess 
                                                 on m.中间体ID equals n.中间体ID                                                   
                                                 where n.可用 == false 
                                                 && m.会计期间ID == Application["accountPeriod"].ToString()
                                              select m;
                db.TABC_JS_ProductCostDriver.DeleteAllOnSubmit(DelUnusedPIPDriverAccount);

                                                 
                db.SubmitChanges();
             
                alert = "<script>alert('更新数据成功');</script>";
            }
            catch
            {
                alert = "<script>alert('更新数据失败');</script>";
            }
        }

        protected void DropDownList_CostCenter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Panel_GridList.Visible == true)
            {
                Panel_GridList.Visible = false;
            }
        }

        //定义全局变量，并对列数据绑定事件进行编程。以显示汇总数据
        decimal totalcash = 0;
        protected void GridView_DriverList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                totalcash += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "动因量"));
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "合计";
                e.Row.Cells[2].Text = totalcash.ToString("0.00");
            }
        }






    }
}
